<?php
/**
 * Created by PhpStorm.
 * User: HS032
 * Date: 2020/7/31
 * Time: 10:33
 */

namespace pay\libs;


use pay\modules\frontend\models\Application;
use yii\base\Exception;

class JudgeSign
{
    protected $appLicat = '';

    /*** 查询applicat
     * @param $aid
     * @return bool
     */
    public function setAppLicat($aid)
    {
       $applicat = $this->getApplyInfo($aid);
       if  (!empty($applicat)){
           $this->appLicat  = $applicat;
           return true;
       }else{
           return false;
       }
    }

    /*** 支付校验签名
     * @param $data
     * @return array
     */
    public function signPay($data)
    {
        if (!isset($data['channelMerId']) || empty($data['channelMerId'])){
            throw new Exception('签名有误，channelMerId',10044);
        }
        if (!isset($data['appId']) || empty($data['appId'])){
            throw new Exception('签名有误，appId',10044);
        }
        if (!isset($data['channelId']) || empty($data['channelId'])){
            throw new Exception('签名有误，channelId',10044);
        }
        if (!isset($data['nonceStr']) || empty($data['nonceStr'])){
            throw new Exception('签名有误，nonceStr',10044);
        }
        if (!isset($data['timestamp']) || empty($data['timestamp'])){
            throw new Exception('签名有误，timestamp',10044);
        }
        if (!isset($data['callBackUrl']) || empty($data['callBackUrl'])){
            throw new Exception('签名有误，callBackUrl',10044);
        }
        if (!isset($data['amount']) || $data['amount'] < 0 ){
            throw new Exception('签名有误，amount',10044);
        }
        if (!isset($data['payNo']) || empty($data['payNo'])){
            throw new Exception('签名有误，payNo',10044);
        }
        if (!isset($data['aid']) || empty($data['aid'])){
            throw new Exception('签名有误，aid',10044);
        }

        $timestamp = isset($data['timestamp'])?$data['timestamp']:'';
        $nonceStr = isset($data['nonceStr'])?$data['nonceStr']:'';
        $checkTimeStr = $this->checkTimeStr($timestamp,$nonceStr);
        if ($checkTimeStr['code'] != 0){
            throw new Exception('签名有误，sign',40045);
        }
        $this->setAppLicat($data['aid']);
        return true;
    }


    /*** 退款校验签名
     * @param $data
     * @return array
     */
    public function signRefund($data)
    {
        if (!isset($data['nonceStr']) || empty($data['nonceStr'])){
            throw new Exception('签名有误，nonceStr',10044);
        }
        if (!isset($data['timeStamp']) || empty($data['timeStamp'])){
            throw new Exception('签名有误，timeStamp',10044);
        }
        if (!isset($data['appId']) || empty($data['appId'])){
            throw new Exception('签名有误，appId',10044);
        }
        if (!isset($data['aid']) || empty($data['aid'])){
            throw new Exception('签名有误，aid',10044);
        }
        if (!isset($data['payUserId']) || empty($data['payUserId'])){
            throw new Exception('签名有误，payUserId',10044);
        }
        if (!isset($data['payUserSerial']) || empty($data['payUserSerial'])){
            throw new Exception('签名有误，payUserSerial',10044);
        }
        if (!isset($data['totalFee']) || empty($data['totalFee'])){
            throw new Exception('签名有误，totalFee',10044);
        }
        if (!isset($data['refundFee']) || empty($data['refundFee'])){
            throw new Exception('签名有误，refundFee',10044);
        }
        if (!isset($data['sign']) || empty($data['sign'])){
            throw new Exception('签名有误，sign',10044);
        }
        $timestamp = isset($data['timestamp'])?$data['timestamp']:'';
        $nonceStr = isset($data['nonceStr'])?$data['nonceStr']:'';
        $checkTimeStr = $this->checkTimeStr($timestamp,$nonceStr,'refund');
        if ($checkTimeStr['code'] !== 0){
            throw new Exception('签名有误，sign',40045);
        }
        $this->setAppLicat($data['aid']);
        return true;
    }


    /*** 查询订单校验参数
     * @param $data
     * @return array
     */
    public function signViewOrder($data)
    {
        if (!isset($data['appId']) || empty($data['appId'])){
            throw new Exception('签名有误，appId',10044);
        }
        if (!isset($data['nonceStr']) || empty($data['nonceStr'])){
            throw new Exception('签名有误，nonceStr',10044);
        }
        if (!isset($data['timestamp']) || empty($data['timestamp'])){
            throw new Exception('签名有误，timestamp',10044);
        }
        if (!isset($data['aid']) || empty($data['aid'])){
            throw new Exception('签名有误，aid',10044);
        }
        if (!isset($data['payNo']) || empty($data['payNo'])){
            throw new Exception('签名有误，payNo',10044);
        }
        $this->setAppLicat($data['aid']);
        return true;
    }

    /*** 支付宝支付 校验参数
     * @param $data
     * @return bool
     * @throws Exception
     */
    public function signAliPay($data)
    {
        if (!isset($data['channelMerId']) || empty($data['channelMerId'])){
            throw new Exception('签名有误，channelMerId','10044');
        }
        if (!isset($data['appId']) || empty($data['appId'])){
            throw new Exception('签名有误，appId','10044');
        }
        if (!isset($data['nonceStr']) || empty($data['nonceStr'])){
            throw new Exception('签名有误，nonceStr','10044');
        }
        if (!isset($data['timestamp']) || empty($data['timestamp'])){
            throw new Exception('签名有误，timestamp','10044');
        }
        if (!isset($data['amount']) || empty($data['amount'])){
            throw new Exception('签名有误，appId','10044');
            return ['code' => 10044,'msg' => '签名有误，amount'];
        }
        if (!isset($data['payNo']) || empty($data['payNo'])){
            throw new Exception('签名有误，payNo','10044');
        }
        if (!isset($data['aid']) || empty($data['aid'])){
            throw new Exception('签名有误，aid','10044');
        }
        if (!isset($data['callBackUrl']) || empty($data['callBackUrl'])){
            throw new Exception('签名有误，callBackUrl','10044');
        }
        if (!isset($data['returnUrl']) || empty($data['returnUrl'])){
            throw new Exception('签名有误，returnUrl','10044');
        }
        if (!isset($data['tradeType']) || empty($data['tradeType'])){
            throw new Exception('签名有误，tradeType','10044');
        }
        if (!isset($data['subject']) || empty($data['subject'])){
            throw new Exception('签名有误，subject','10044');
        }
        if (!isset($data['from']) || empty($data['from'])){
            throw new Exception('签名有误，from','10044');
        }

        $this->setAppLicat($data['aid']);
        return true;

    }



    public function signAliRefund($data)
    {
        if (!isset($data['channelMerId']) || empty($data['channelMerId'])){
            throw new Exception('签名有误，channelMerId','10044');
        }
        if (!isset($data['appId']) || empty($data['appId'])){
            throw new Exception('签名有误，appId','10044');
        }
        if (!isset($data['aid']) || empty($data['aid'])){
            throw new Exception('签名有误，aid','10044');
        }
        if (!isset($data['payUserId']) || empty($data['payUserId'])){
            throw new Exception('签名有误，payUserId','10044');
        }
        if (!isset($data['nonceStr']) || empty($data['nonceStr'])){
            throw new Exception('签名有误，nonceStr','10044');
        }
        if (!isset($data['timestamp']) || empty($data['timestamp'])){
            throw new Exception('签名有误，timestamp','10044');
        }
        if (!isset($data['totalFee']) || empty($data['totalFee'])){
            throw new Exception('签名有误，totalFee','10044');
        }
        if (!isset($data['refundFee']) || empty($data['refundFee'])){
            throw new Exception('签名有误，refundFee','10044');
        }
        if (!isset($data['payNo']) || empty($data['payNo'])){
            throw new Exception('签名有误，payNo','10044');
        }
        if (!isset($data['refundUserSerial']) || empty($data['refundUserSerial'])){
            throw new Exception('签名有误，refundUserSerial','10044');
        }
//        if (!isset($data['returnUrl']) || empty($data['returnUrl'])){
//            throw new Exception('签名有误，returnUrl','10044');
//        }
//        if (!isset($data['sign']) || empty($data['sign'])){
//            throw new Exception('签名有误，sign','10044');
//        }
        $this->setAppLicat($data['aid']);
        return true;
    }


















    /*** 获取加密key
     * @param $app_id
     * @return array|bool|mixed|null|\yii\db\ActiveRecord
     */
    private function getApplyInfo($aid)
    {
        if (!empty($aid)){
            $applicatModel = new Application();
            $a_key = $applicatModel->getApplyKey($aid,2);
            if (!empty($a_key)){
                return $a_key;
            }
        }
        return false;
    }


    /*** 验证时间戳与随机字符串， 时间戳必须是小于3分钟的，随机字符串要保证请求时间100内保持唯一；
     * @param $time
     * @param $str
     * @return array
     */
    private function checkTimeStr($time,$str,$type = 'pay')
    {
//        if (time() > (int)$time + 300){
//            return ['code' => 99,'msg' => '支付时间戳过期'];
//        }
//        $cache = \Yii::$app->cache;
//        $status = $cache->get($type.$str);
//        if ($status){
//            return ['code' => 99,'msg' => '随机字符串已使用过'];
//        }else{
//            $cache->set($type.$str,'1',100);
//        }
        return ['code' => 0,'msg' => 'ok'];

    }



    /*** AES-128-CBC，解密
     * @param $data   解密数据
     * @param $key    key
     * @param $iv     向量
     * @return string
     */
    protected function decryption($data,$key,$iv)
    {
        $res = openssl_decrypt($data,"AES-128-CBC",$key,0,$iv);
        return $res;
    }

    /**** 加密
     * @param $data
     * @param $key
     * @param $iv
     * @return string
     */
    protected function encrypt($data,$key,$iv)
    {
        $res =  openssl_encrypt($data, "AES-128-CBC", $key,0, $iv);
        return $res;
    }


}